from django.http import HttpResponseBadRequest, HttpResponseRedirect
from django.shortcuts import render

# Create your views here.
from django.views import View
from django.views.generic import RedirectView

from apps.category.models import Tags, Article
from apps.comments.models import Comments
from apps.links.models import Links
from apps.users.models import Users


class Tag(View):
    def get(self, request):

        try:
            tags = Tags.objects.all()
        except:
            return HttpResponseBadRequest('发生了不可预知的错误')

        else:
            context = {
                'tags': tags
            }
        return render(request, 'tags.html', context=context)


class Reder(View):
    """reader wall"""

    def get(self, request):
        try:
            users = Users.objects.all()


        except:
            return HttpResponseBadRequest('发生了不可预知的错误')
        else:
            user_comment = {}  # count and  user

            for user in users:
                count = user.comments_set.count()

                user_comment[count] = user.username

            comment_count = sorted(user_comment,
                                   reverse=True)  # these result is that sort comment's count and  this is a list but not is a dict ，the dict just only involve number
            count_comment = {}  # count and type
            dicts = ['金牌读者', '银牌读者', '铜牌读者', '塑料读者']
            if len(comment_count) > 4:
                for num, count1 in enumerate(comment_count[0:5]):
                    count_comment[count1] = dicts[num]
            else:
                for num, count1 in enumerate(comment_count):
                    count_comment[count1] = dicts[num]

        context = {'count_comment': count_comment,
                   'dicts': dicts,
                   'user_comment': user_comment
            , }

        return render(request, 'readers.html', context=context)


class Link(View):
    def get(self, request):
        try:
            links = Links.objects.all()
        except:
            return HttpResponseBadRequest('发生了不可预知的错误')




        return render(request, 'links.html', locals())



#
# class Skip(RedirectView):
#
#     url = ''
#     def __init__(self,link):
#         self.url=link[link]




# class Skip(View):
#     def get(self, link2):
#         global link1
#         link1 = link2


